﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Netstars.SessionCache
{
    /// <summary>
    /// <see cref="Netstars.SessionCache.SessionCache"/> 对象的文件缓存依赖
    /// </summary>
    public class CacheDependency
    {
        #region 公共属性
        /// <summary>
        /// 获取文件缓存依赖中已存在的文件列表
        /// </summary>
        public string[] FilePath { private set; get; } 
        #endregion

        #region 构造函数
        /// <summary>
        /// 初始化文件缓存依赖类
        /// </summary>
        public CacheDependency()
        {
            
        }
        
        /// <summary>
        /// 初始化文件缓存依赖类
        /// </summary>
        /// <param name="filePath">指定的一个文件地址</param>
        public CacheDependency(string filePath)
        {
            FilePath = new[] { filePath };
        }
        /// <summary>
        /// 初始化文件缓存依赖类
        /// </summary>
        /// <param name="filePaths">多个文件地址数组</param>
        public CacheDependency(string[] filePaths)
        {
            FilePath = filePaths;
        }
        /// <summary>
        /// 初始化文件缓存依赖类
        /// </summary>
        /// <param name="collection"></param>
        public CacheDependency(IEnumerable<string> collection)
        {
            var enumerable = collection as string[] ?? collection.ToArray();
            if (collection == null || enumerable.Any())
            {
                FilePath = enumerable.ToArray();
            }
        }

        #endregion

        #region 添加一个文件地址到缓存依赖对象中
        /// <summary>
        /// 添加一个文件地址到缓存依赖对象中
        /// </summary>
        /// <param name="filePath">一个文件物理地址</param>
        public void Add(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException("filePath", "文件地址不能为空!");
            }
            if (FilePath == null || FilePath.Length <= 0)
            {
                FilePath = new[] { filePath };
            }
            else
            {
                FilePath[FilePath.Length + 1] = filePath;
            }
        } 
        #endregion

        #region 将一组文件物理路径列表添加到文件依赖中
        /// <summary>
        /// 将一组文件物理路径列表添加到文件依赖中
        /// </summary>
        /// <param name="collection"></param>
        public void AddRange(IEnumerable<string> collection)
        {
            foreach (var item in collection)
            {
                if (string.IsNullOrWhiteSpace(item) == false)
                {
                    if (FilePath == null || FilePath.Length <= 0)
                    {
                        FilePath = new[] { item };
                    }
                    else
                    {
                        FilePath[FilePath.Length + 1] = item;
                    }
                }
            }
        } 
        #endregion
    }
}
